import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
import * as d3 from "d3";

Vue.use(Vuex);

// 用户登录信息存储
const userinfoData = {
  namespaced: true,
  state: {
    userInfo: null,
    username: "",
    currentPath: "",
  },
  actions: {},
  mutations: {
    updateUserInfo(state, payload) {
      // console.log("payload", state, payload);
      state.userInfo = payload;
      state.username = payload.adminname;
    },
    updateCurrentPath(state, payload) {
      state.currentPath = payload;
    },
  },
  getters: {},
};

// workbench 搜索表单信息存储
const searchForm = {
  namespaced: true,
  state: {
    formState: {
      name: "",
      phone: "",
      depa: "",
      date1: "",
      date2: "",
    },
  },
  actions: {},
  mutations: {
    updatesearchForm(state, payload) {
      // console.log("payload", state, payload);
      state.formState = payload;
      // console.log("state", state.formState);
    },
  },
  getters: {
    changeFormData(state) {
      // if (state.formState.date1 === "") {
      //   return state.formState;
      // }

      if (state.formState.date1 !== "") {
        state.formState.date1 = d3.timeFormat("%Y-%m-%d")(
          state.formState?.date1
        );
      }
      if (state.formState.date2 !== "") {
        state.formState.date2 = d3.timeFormat("%H:%M:%S")(
          state.formState?.date2
        );
      }

      // 处理时间对象为可读时间，截取日期和时间

      // state.formState.date2 = d3.timeFormat("%H:%M:%S")(state.formState?.date2);

      return state.formState;
    },
  },
};

export default new Vuex.Store({
  modules: {
    userinfoData,
    searchForm,
  },
  // plugins: [createPersistedState()],
  plugins: [
    createPersistedState({
      storage: window.sessionStorage,
      reducer(val) {
        return {
          userinfoData: val.userinfoData,
        };
      },
    }),
  ],
});
